package com.ahpu.jym.messagealert.tools;


import net.rim.device.api.system.ApplicationDescriptor;
import net.rim.device.api.system.ApplicationManager;
import net.rim.device.api.system.Characters;
import net.rim.device.api.system.EventInjector;
import net.rim.device.api.system.EventInjector.NavigationEvent;
import net.rim.device.api.system.KeypadListener;
import net.rim.device.api.system.EventInjector.KeyCodeEvent;
import net.rim.device.api.ui.Keypad;

public class SmsWork {

	public static void Reply() {

		new Thread() {
			public void run() {

				ApplicationManager appMan = ApplicationManager
						.getApplicationManager();

				ApplicationDescriptor[] c = appMan.getVisibleApplications();
				for (int i = 0; i < c.length; i++) {
					if ("Messages".equals(c[i].getName())) {
						appMan.requestForeground(appMan.getProcessId(c[i]));
						break;
					}
				}

				new Thread() {
					public void run() {
						EventInjector.invokeEvent(new KeyCodeEvent(
								KeyCodeEvent.KEY_DOWN, 'T',
								KeypadListener.STATUS_NOT_FROM_KEYPAD));
						EventInjector.invokeEvent(new KeyCodeEvent(
								KeyCodeEvent.KEY_UP, 'T',
								KeypadListener.STATUS_NOT_FROM_KEYPAD));

						EventInjector
								.invokeEvent(new EventInjector.NavigationEvent(
										NavigationEvent.NAVIGATION_MOVEMENT, 0,
										1,
										KeypadListener.STATUS_NOT_FROM_KEYPAD));

						EventInjector.invokeEvent(new KeyCodeEvent(
								KeyCodeEvent.KEY_DOWN, (char) Keypad.KEY_ENTER,
								KeypadListener.STATUS_NOT_FROM_KEYPAD));
						EventInjector.invokeEvent(new KeyCodeEvent(
								KeyCodeEvent.KEY_UP, (char) Keypad.KEY_ENTER,
								KeypadListener.STATUS_NOT_FROM_KEYPAD));
					};
				}.start();

			};
		}.start();

	}

	public static void mark() {

		new Thread() {
			public void run() {

				ApplicationManager appMan = ApplicationManager
						.getApplicationManager();

				ApplicationDescriptor[] c = appMan.getVisibleApplications();
				for (int i = 0; i < c.length; i++) {
					if ("Messages".equals(c[i].getName())) {
						appMan.requestForeground(appMan.getProcessId(c[i]));
						break;
					}
				}

				new Thread() {
					public void run() {
						EventInjector.invokeEvent(new KeyCodeEvent(
								KeyCodeEvent.KEY_DOWN, 'T',
								KeypadListener.STATUS_NOT_FROM_KEYPAD));
						EventInjector.invokeEvent(new KeyCodeEvent(
								KeyCodeEvent.KEY_UP, 'T',
								KeypadListener.STATUS_NOT_FROM_KEYPAD));

						EventInjector
								.invokeEvent(new EventInjector.NavigationEvent(
										NavigationEvent.NAVIGATION_MOVEMENT, 0,
										1,
										KeypadListener.STATUS_NOT_FROM_KEYPAD));

						

						EventInjector.invokeEvent(new KeyCodeEvent(
								KeyCodeEvent.KEY_DOWN, 'U',
								KeypadListener.STATUS_ALT));

						EventInjector.invokeEvent(new KeyCodeEvent(
								KeyCodeEvent.KEY_UP, 'U',
								KeypadListener.STATUS_ALT));

						if (OptionsData.load().getGomessages() != 0) {
							EventInjector.invokeEvent(new KeyCodeEvent(
									KeyCodeEvent.KEY_DOWN, Characters.ESCAPE,
									0));
							EventInjector.invokeEvent(new KeyCodeEvent(
									KeyCodeEvent.KEY_UP, Characters.ESCAPE,
									0));

							
						}
					};
				}.start();

			};
		}.start();

	}

	public static void Forward() {
		new Thread() {
			public void run() {

				ApplicationManager appMan = ApplicationManager
						.getApplicationManager();
				ApplicationDescriptor[] c = appMan.getVisibleApplications();
				for (int i = 0; i < c.length; i++) {
					if ("Messages".equals(c[i].getName())) {
						appMan.requestForeground(appMan.getProcessId(c[i]));
						break;
					}
				}

				new Thread() {
					public void run() {
						EventInjector.invokeEvent(new KeyCodeEvent(
								KeyCodeEvent.KEY_DOWN, 'T',
								KeypadListener.STATUS_NOT_FROM_KEYPAD));

						EventInjector.invokeEvent(new KeyCodeEvent(
								KeyCodeEvent.KEY_UP, 'T',
								KeypadListener.STATUS_NOT_FROM_KEYPAD));

						EventInjector
								.invokeEvent(new EventInjector.NavigationEvent(
										NavigationEvent.NAVIGATION_MOVEMENT, 0,
										1,
										KeypadListener.STATUS_NOT_FROM_KEYPAD));

						EventInjector.invokeEvent(new KeyCodeEvent(
								KeyCodeEvent.KEY_DOWN, 'F',
								KeypadListener.STATUS_NOT_FROM_KEYPAD));

						EventInjector.invokeEvent(new KeyCodeEvent(
								KeyCodeEvent.KEY_UP, 'F',
								KeypadListener.STATUS_NOT_FROM_KEYPAD));
					};
				}.start();

			};
		}.start();
	}

}
